import request from "@/utils/request";
import { rootsUrl } from "../utils";
import { getToken } from "@/utils/auth";
import { parseTime } from "@/utils";
import Qs from "qs";

/**
 * 获取在线学习课程
 * @param {*} tag 受众人群 id可以多个  格式字符串逗号隔开 例如:48,50
 * @param {*} pageIndex
 */
export const getCourseList = (tag, pageIndex) => {
  return request({
    url: `${rootsUrl[1]}/cbeducate/course/getCourseList.shtml`,
    method: "get",
    params: {
      tag,
      pageIndex,
      pageSize: 10
    }
  });
};

/**
 * 获取在线学习受众人群
 */
export const getCourseTags = () => {
  return request({
    url: `${rootsUrl[1]}/cbeducate/course/getOnlineLearnTypeList.shtml`,
    method: "get"
  });
};

/**
 * 获取课程详情
 * @param {*} id 课程ID
 * @param {*} userId 用户ID
 */
export const getCourseDetail = (id, userId) => {
  return request({
    url: `${rootsUrl[1]}/cbeducate/course/getCourseDetails.shtml`,
    method: "get",
    params: {
      id,
      userId
    }
  });
};

/**
 * 报名课程
 * @param {*} id 课程ID
 * @param {*} userId
 * @param {*} userName
 */
export const signUp = (id, userId, userName) => {
  let token = getToken();
  return request({
    url: `${rootsUrl[1]}/cbeducate/course/addCourseLearnNum.shtml`,
    method: "get",
    params: {
      nid: id,
      userId,
      userName,
      token
    }
  });
};

/**
 * 获取在线课程章节列表
 * @param {Number} id
 */
export const getLessonList = id => {
  let token = getToken();
  return request({
    url: `${rootsUrl[1]}/cbeducate/courseMenu/getLessonList.shtml`,
    method: "get",
    params: {
      nid: id
    }
  });
};

/**
 * 获取课程留言
 * @param {*} courseId 课程Id
 * @param {*} classId 节Id
 */
export const getCommentList = (courseId, classId) => {
  let token = getToken();
  return request({
    url: `${rootsUrl[1]}/cbeducate/courseQuestion/getCommentList.shtml`,
    method: "get",
    params: {
      courseId,
      classId
    }
  });
};

/**
 * 提交在线留言
 * @param {*} obj
 */
export const postComment = obj => {
  let token = getToken();
  let data = {
    token,
    courseId: obj.courseId,
    classId: obj.classId,
    mid: obj.userId,
    mname: obj.userName,
    content: obj.content
  };
  return request({
    url: `${rootsUrl[1]}/cbeducate/courseQuestion/addComment.shtml`,
    method: "post",
    headers: {
      "Content-type": "application/x-www-form-urlencoded"
    },
    data: Qs.stringify(data)
  });
};

/**
 * 获取课节题目
 * @param {*} userId 用户ID
 * @param {*} classId 节ID
 */
export const getQuestionList = (userId, classId) => {
  let token = getToken();
  return request({
    url: `${rootsUrl[1]}/cbeducate/courseQuestion/getQuestionListById.shtml`,
    method: "get",
    params: {
      userId,
      classId,
      token
    }
  });
};

/**
 * 提交答案
 * @param {Object} ids userID 用户,courseId 课程，chapterId 章，partId节，
 * @param {Array} answers
 */
export const postAnswer = (ids, answers) => {
  let token = getToken();
  let info = {
    token,
    userId: ids.userId,
    courseId: ids.courseId,
    lessonId: ids.chapterId,
    classId: ids.partId,
    data: answers
  };
  let data = {
    info: JSON.stringify(info)
  };
  return request({
    url: `${rootsUrl[1]}/cbeducate/courseQuestion/saveQuestionListAnser.shtml`,
    method: "post",
    headers: {
      "Content-type": "application/x-www-form-urlencoded"
    },
    data: Qs.stringify(data)
  });
};

/**
 * 提交答案
 * @param {Object} ids userID 用户,courseId 课程，chapterId 章，partId节，
 * @param {Array} answers
 */
export const getUserMedal = userId => {
  let token = getToken();
  let data = {
    token,
    userId
  };
  return request({
    url: `${rootsUrl[1]}/cbeducate/courseQuestion/getUserMedal.shtml?`,
    method: "post",
    headers: {
      "Content-type": "application/x-www-form-urlencoded"
    },
    data: Qs.stringify(data)
  });
};
